<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../assets/js/vue.js"></script>
    <title>vue.directive 自定义指令</title>
</head>
<body>
    <h1>vue.directive 自定义指令</h1>
    <hr>
    <div id="app">
       <div v-jspang="color" id="aaa">
           {{num}}
       </div>
       <p>
           <button @click='jia'>加分</button>
       </p>
       <p>
           <button onclick='unbind()'>解绑</button>
       </p>
    </div>

    <script type="text/javascript">

        function unbind(){
            app.$destroy();
        }

        //自定义指令
        Vue.directive('jspang',{
            bind:function(el,binding,vnode){//被绑定
                /**
                        var s=JSON.stringify;
                        el.innerHTML = 
                            'name:'        + s(binding.name) +'<br>' + 
                            'value:'       + s(binding.value) +'<br>' + 
                            'expression:'  + s(binding.expression) +'<br>' ;
                **/
                el.style='color:'+binding.value;
                  

                console.log('1 - bind');
            },
            inserted:function(){//绑定到节点
                console.log('2 - inserted');
            },
            update:function(){//组件更新
                console.log('3 - update');
            },
            componentUpdated:function(){//组件更新完成
                console.log('4 - componentUpdated');
            },
            unbind:function(){//解绑
                console.log('5 - unbind');
            }

        })

        var app=new Vue({
            el:'#app',
            data:{
                color:'green',
                num:10
            },
            methods:{
                jia:function(){
                    this.num++;
                }
            }
        })
    </script>
</body>
</html>